package net.wofly.sale.commodity.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.yunpian.sdk.util.StringUtil;

import net.wofly.common.condition.Condition;
import net.wofly.common.condition.ConditionRelate;
import net.wofly.common.condition.ConditionUtil;
import net.wofly.common.condition.FilterCondition;
import net.wofly.common.condition.Operation;
import net.wofly.common.condition.OrderCondition;
import net.wofly.common.json.XiNiaoJSON;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.util.PageHelper;
import net.wofly.framework.util.ReturnBean;
import net.wofly.sale.commodity.domain.Commodity;
import net.wofly.sale.commodity.domain.CommodityType;
import net.wofly.sale.commodity.repository.CommodityRepository;
import net.wofly.sale.commodity.repository.CommodityTypeRepository;
import net.wofly.sale.commodity.service.ICommodityTypeService;

@Service("iCommodityTypeService")
@Transactional
public class CommodityTypeServiceImpl implements ICommodityTypeService {
	
	private String defaultCommodityTypeID = "1000" ;

	@Autowired
	CommodityTypeRepository commodityTypeRepository;
	
	@Autowired
	CommodityRepository commodityRepository ;

	@Override
	public ReturnBean<CommodityType> save(CommodityType commodityType) {
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS ,"保存商品类型成功！" ,commodityTypeRepository.save(commodityType));
	}

	@Override
	public ReturnBean<String> delete(CommodityType commodityType) {
		CommodityType dbCommodityType = commodityTypeRepository.findOne(commodityType.getCommodityTypeID()) ;
		if (null == dbCommodityType) {
			return new ReturnBean<>(-1 ,"欲删除的商品类型不存在！");
		}
		
		List<Commodity> commoditys = commodityRepository.findByCommodityTypeID(commodityType.getCommodityTypeID()) ;
		if (null==commoditys || commoditys.isEmpty()) {
			commodityTypeRepository.delete(commodityType);
		}else {
			dbCommodityType.setModifyDate(System.currentTimeMillis());
			dbCommodityType.setStatus(0);
			commodityTypeRepository.save(dbCommodityType) ;
		}
		
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS  ,"删除商品类型成功！");
	}

	@Override
	public ReturnBean<String> delete(String commodityTypeID) {
		CommodityType dbCommodityType = commodityTypeRepository.findOne(commodityTypeID) ;
		if (null == dbCommodityType) {
			return new ReturnBean<>(-1 ,"欲删除的商品类型不存在！");
		}
		
		List<Commodity> commoditys = commodityRepository.findByCommodityTypeID(commodityTypeID) ;
		if (null==commoditys || commoditys.isEmpty()) {
			commodityTypeRepository.delete(commodityTypeID);
		}else {
			dbCommodityType.setModifyDate(System.currentTimeMillis());
			dbCommodityType.setStatus(0);
			commodityTypeRepository.save(dbCommodityType) ;
		}
		
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS  ,"删除商品类型成功！");
	}

	@Override
	public CommodityType findOne(String commodityTypeID) {
		return commodityTypeRepository.findOne(commodityTypeID);
	}

	@Override
	public ReturnBean<List<CommodityType>> save(List<CommodityType> entityList) {
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS  ,"获取商品类型成功！",(List<CommodityType>) commodityTypeRepository.save(entityList));
	}

	@Override
	public ReturnBean<PageWrapper<CommodityType>> findJiuMaoCommodityTypeByPage(String commodityTypeName ,Integer pageNum, Integer pageSize) {
		Condition condition = new Condition();
        List<FilterCondition> filterConditions = new ArrayList<>();
        List<OrderCondition> orderConditions = new ArrayList<>();
        
    		filterConditions.add(new FilterCondition("1", "status", ConditionRelate.and.name(), Operation.eq.name(), 1)) ;
    		filterConditions.add(new FilterCondition("10", "commodityTypeID", ConditionRelate.and.name(), Operation.gt.name(), 2)) ;
    		filterConditions.add(new FilterCondition("1999", "commodityTypeID", ConditionRelate.and.name(), Operation.lt.name(), 3)) ;
    		
    		if (StringUtils.isNotEmpty(commodityTypeName)) {
    			filterConditions.add(new FilterCondition(commodityTypeName, "commodityTypeName", ConditionRelate.and.name(), Operation.like.name(), 4)) ;
    		}
    		
    		orderConditions.add(new OrderCondition("modifyDate", "desc" ,1)) ;
    		
    		condition.setFilterCondition(filterConditions);
    		condition = ConditionUtil.buildCondition(XiNiaoJSON.toJSONString(condition), CommodityType.class).getResult();
    		
		Pageable pageable = new PageRequest(pageNum - 1,pageSize);
		
		Page<CommodityType> page = commodityTypeRepository.findAll(condition.getSpecification() ,pageable) ;
		 
		return  new ReturnBean<>(ReturnBean.CODE_SUCCESS , "获取商品类型列表成功！" , PageHelper.toPage(page)) ;
		
	}

	@Override
	public ReturnBean<CommodityType> saveCommodityType(CommodityType commodityType) {
		String maxCommodityID = commodityTypeRepository.getMaxCommodityID() ;
		if(StringUtil.isNullOrEmpty(maxCommodityID)) {
			maxCommodityID = this.defaultCommodityTypeID ;
		}
		commodityType.setCommodityTypeID(String.valueOf(Integer.parseInt(maxCommodityID) +1));
		commodityType.setStatus(1);
		commodityType.setModifyDate(System.currentTimeMillis());
		
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS  ,"保存商品类型成功！" , commodityTypeRepository.save(commodityType));
	}

	@Override
	public ReturnBean<CommodityType> updateCommodityType(CommodityType commodityType) {
		CommodityType dbCommodityType = commodityTypeRepository.findOne(commodityType.getCommodityTypeID()) ;
		if (null == dbCommodityType) {
			return new ReturnBean<>(-1 ,"欲修改的商品类型不存在！");
		}
		dbCommodityType.setCommodityTypeName(commodityType.getCommodityTypeName());
		dbCommodityType.setStatus(1);
		dbCommodityType.setModifyDate(System.currentTimeMillis());
		
		commodityTypeRepository.save(dbCommodityType) ;
		
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS  ,"修改商品类型成功！");
	}

	@Override
	public ReturnBean<List<CommodityType>> findJiuMaoCommodityType() {
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS  ,"获取商品类型成功！" ,commodityTypeRepository.findJiuMaoCommodityType() );
	}
}
